<?php
/**
 * Created by PhpStorm.
 * User: Mr.Zhu
 * Date: 2021-2-25
 * Time: 18:48
 */


class Client {

	private $userId = '系统分配的userId';


    /**
     * 获取token
     * @param  $request
     */
    public function getToken($request) {
		//$request = $_GET;
		$url = 'http://xxx/idauth/token/getToken';
        return $this->request($url, $request);
    }

    /**
     * 3要素认证
     * @param $request
     */
    public function threee($request) {
		
		$token = 'Authorization: Bearer '.'';	// 通过getToken获取到的token值后拼接
		$url = 'http://XXX/idauth/auth/3e';
		$signstr = 'facePhotoBase64='.$request['facePhotoBase64'].'&idNumber='.$request['idNumber'].'&name='.$request['name'].'&'.$userId;
		$sign = md5($signstr);
		echo $signstr.'------'.$sign;
		$request['sign'] = $sign;
        return $this->request($url, $request, $token);
    }


    private function request($url, $params, $token='') {
		
		$header=array('Content-Type: application/x-www-form-urlencoded', $token);	// 这里很重要
        $config = [
			CURLOPT_HTTPHEADER => $header,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_AUTOREFERER => true,
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_URL => $url,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
        ];
        $curl = curl_init();
        foreach ($config as $key => $value) {
            curl_setopt($curl, $key, $value);
        }
        $c = curl_exec($curl);
        $data = json_decode($c, true);
        return $data;
    }
	
	
}

	function main() {
		echo '开始了。。。';
		$req = array('clientId'=>'系统分配的clientId', 'clientSecret'=>'系统分配的clientSecret');
		//$req = array('name'=>'姓名', 'idNumber'=>'身份证', 'facePhotoBase64'=>'照片base64编码');
		$Client = new Client();
		$data = $Client->getToken($req);
		//$data = $Client->threee($req);
		print_r($data);
		echo '结束了。。。';
		exit;
	}
	
	main();
?>
